package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.common.UserManger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 两人对话
 * 
 * @author geliang
 * 
 */
public class CostJinBiListFragment extends BaseListFragmet {

	private TextView empty;
	private ListView list;
	protected String NOTFOUND = "似乎没有消费";
	private Drawable drawable;

	private void bindViews() {
		empty = (TextView) findViewById(R.id.empty);
		list = (ListView) findViewById(android.R.id.list);
		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});
		empty.setText(NOTFOUND);
		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.listview_empty, container,
				false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_talking_btn_mail_p);
		bindViews();
		initdata();
		return rootView;
	}

	private void initdata() {
		initAbsListView(getListView(), API.URL + "GetMoneyList?userid=%s",
				new Object[] { UserManger.getInstance().userid }, R.layout.item_list_costpost,
				new String[] { "logcontent","actiondate","actiontype","amount" }, new int[] { R.id.tv_nick,R.id.age,R.id.time,R.id.count });

	}
	@Override
	protected String preFormatJsonData(String t) {
		JSONArray ja;
		try {
			final JSONObject jsonObject = new JSONObject(t);
			ja = jsonObject.getJSONArray("data");
			JSONArray newja = new JSONArray();
			for (int i = 0; i < ja.length(); i++) {
				// age/height/education/jobs
				JSONObject jo = ja.getJSONObject(i);
				int action = jo.optInt("actiontype");
				jo.put("actiontype", action==1?"增加":"减少");
				newja.put(jo);
			}
			jsonObject.put("data", newja);
			return jsonObject.toString();
		} catch (JSONException e) {
			e.printStackTrace();
			return t;
		}
	}
	@Override
	public Object getOnItemListener(String[] from, String t)
			throws JSONException {
		return null;
	}
}